package org.homeworkadmin.admin.controller;

import org.homeworkadmin.admin.domain.Video;
import org.homeworkadmin.admin.service.VideoService;
import org.homeworkadmin.admin.utils.FileUploadUtils;
import org.homeworkadmin.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.io.IOException;
@RestController
@RequestMapping("/video")
public class VedioController {
    @Autowired
    private VideoService videoService;
public static String uploadDir = "E:\\files";
    @PostMapping("/videoSave")
    public String handleFileUpload(@RequestParam("video_img") MultipartFile file,
                                   RedirectAttributes redirectAttributes) {

        System.out.println("file:" + file);
        System.out.println("文件原始名称: " + file.getOriginalFilename());

        try {
            String filePath = FileUploadUtils.saveFile(file, uploadDir);
            redirectAttributes.addFlashAttribute("message", "文件上传成功");
            return filePath;
        } catch (IOException e) {
            e.printStackTrace();
            System.err.println("文件上传失败: " + e.getMessage());
            redirectAttributes.addFlashAttribute("message", "文件上传失败: " + e.getMessage());
        }

        return "faild"; // 返回重定向到结果页面
    }
    @PostMapping("/addVideo")
    public AjaxResult submit(@RequestBody Video video) {
//        System.out.println(video);
        int re = videoService.addVideo(video);
        if (re!=0) {
            return new AjaxResult(200,"视频发布成功！");
        }
        else {
            return new AjaxResult(500,"视频发布失败！");
        }
    }
}
